package com.wbs.shop.nuby.helper;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wbs.shop.nuby.api.RxSubscribe;
import com.wbs.shop.nuby.view.base.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhoucl on 2018/5/16 0016.
 */
public abstract class RefreshHelper<Result extends List> implements Callback.OnReloadListener, OnRefreshLoadMoreListener {
    private RecyclerView recyclerview;
    private SmartRefreshLayout refreshLayout;
    private BaseRecyclerAdapter mBaseRecyclerAdapter;
    private LoadService mLoadService;
    private int pageNo = 1;

    public RefreshHelper(RecyclerView recyclerview, SmartRefreshLayout refreshLayout, BaseRecyclerAdapter mBaseRecyclerAdapter) {
        this(recyclerview, refreshLayout, null, mBaseRecyclerAdapter);
    }

    public RefreshHelper(RecyclerView recyclerview, SmartRefreshLayout refreshLayout, View loadView, BaseRecyclerAdapter mBaseRecyclerAdapter) {
        this.recyclerview = recyclerview;
        this.mBaseRecyclerAdapter = mBaseRecyclerAdapter;
        this.refreshLayout = refreshLayout;
        mBaseRecyclerAdapter.setDatas(new ArrayList<>());
        recyclerview.setLayoutManager(new LinearLayoutManager(recyclerview.getContext()));
        recyclerview.setAdapter(mBaseRecyclerAdapter);
        refreshLayout.setOnRefreshLoadMoreListener(this);
        mLoadService = LoadSir.getDefault().register(loadView == null ? refreshLayout : loadView, this);
    }

    public RxSubscribe<Result> getRxSubscribe() {
        return new RxSubscribe<Result>(recyclerview.getContext()) {
            @Override
            protected void _onNext(Result result) {
                if (result == null)
                    return;
                if (pageNo == 1) {
                    mBaseRecyclerAdapter.clear();
                }
                if (pageNo != 1 && result.isEmpty())
                    pageNo--;
                if (refreshLayout != null && result.isEmpty())
                    refreshLayout.setNoMoreData(true);
                else {
                    if (refreshLayout != null)
                        refreshLayout.setNoMoreData(false);
                    handResult(result);
//                    int itemCount = mBaseRecyclerAdapter.getItemCount();
                    mBaseRecyclerAdapter.addAll(result);
//                    mBaseRecyclerAdapter.notifyItemRangeInserted(itemCount, mBaseRecyclerAdapter.getItemCount());
//                    mBaseRecyclerAdapter.notifyItemRangeChanged(itemCount - 1, mBaseRecyclerAdapter.getItemCount());
//                if (mBaseRecyclerAdapter != null && !result.isEmpty()) {
//                }
                }
                showView();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (mLoadService != null && mBaseRecyclerAdapter.getItemCount() == 0) {
                    mLoadService.showCallback(ErrorCallback.class);
                }
            }
        }.smartRefreshListener(refreshLayout);
    }

    public void showView() {
        if (mLoadService != null && mBaseRecyclerAdapter.getItemCount() == 0) {
            mLoadService.showCallback(EmptyCallback.class);
        } else if (mLoadService != null) {
            mLoadService.showSuccess();
        }
    }

    public abstract void loadData();

    public void handResult(Result result) {

    }

    @Override
    public void onReload(View v) {
        loadData();
    }

    @Override
    public void onLoadMore(RefreshLayout refreshlayout) {
        pageNo++;
        loadData();
    }

    public void onRefresh() {
        onRefresh(null);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        if (refreshLayout != null)
            refreshLayout.setNoMoreData(false);
        pageNo = 1;
        loadData();
    }

    public int getPageNo() {
        return pageNo;
    }

    public void onDestroy() {
        recyclerview = null;
        mLoadService = null;
        refreshLayout = null;
        mBaseRecyclerAdapter = null;
    }

}
